package com.apes.hr.salary.rewardPunish.model;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import com.apes.scm.rbac.model.User;
import com.apes.scm.rbac.repository.UserRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 功能：奖惩记录单
 * @author HuJianlin
 * @create 2018-05-05 15:11
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = RewardPunish.CONST_TABLE_NAME)
public class RewardPunish extends Model {
    public static final String CONST_TABLE_NAME = "hr_Reward_Punish";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "奖惩记录", prefix = "H", padding = 7)
    @Comment("奖惩记录单号")
    private String id;   //奖惩记录单号

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "reward_punish_type_id", referencedColumnName = "id")
    @Comment("奖惩类型")
    private RewardPunishType rewardPunishType;  // 奖惩类型

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "quota_type", referencedColumnName = "id" )
    @Comment("指标类别")
    private Enumeration quotaType;//指标类别


    @Column(name = "explain", length = 1000)
    @Comment("具体情况说明")
    private String explain;    // 具体情况说明

    @Column(name = "doc_number", length = 32)
    @Comment("奖罚文号")
    private String docNumber;   // 奖罚文号

    @Column(name = "doc_file_name", length = 32)
    @Comment("文件名")
    private String docFileName;   // 文件名

    @Column(name = "state", length = 10, nullable = false)
    @Comment("状态")
    private String state; //状态： ('sent', '提交'), ('done', '已审核')

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审批人")
    private PartyRole approveUid; //审批人

    @Column(name = "approve_date")
    @Comment("审批日期")
    private Date approveDate; //审批日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "send_out_uid", referencedColumnName = "id")
    @Comment("发文人")
    private PartyRole sendOutUid; //发文人

    @Column(name = "send_out_date")
    @Comment("发文日期")
    private Date sendOutDate; //发文日期

    @Column(name = "reward_date")
    @Comment("事发日期起")
    private Date rewardDate; //事发日期起

    @Column(name = "reward_end_date")
    @Comment("事发日期止")
    private Date rewardEndDate; //事发日期止

    @Column(name = "cash_date")
    @Comment("兑现日期")
    private Date cashDate; //兑现日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cancel_uid", referencedColumnName = "id")
    @Comment("作废人")
    private PartyRole cancelUid; //作废人

    @Column(name = "cancel_date")
    @Comment("作废日期")
    private Date cancelDate; //作废日期

    @OneToMany(mappedBy = "rewardPunish", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<RewardPunishItem> rewardPunishItems = new ArrayList<>(); //奖罚单行

    public List<RewardPunishItem> getRewardPunishItems() {
        for (RewardPunishItem item : rewardPunishItems) {
            item.setRewardPunish(this);
        }
        return rewardPunishItems.stream().sorted(Comparator.comparing(rewardPunishItem -> rewardPunishItem.getWorkBranch().getId())).collect(Collectors.toList());
    }
    @Column(name = "source_order")
    @Comment("来源单号")
    private String SourceOrder;
    @Column(name = "source_name")
    @Comment("来源说明")
    private String SourceName;

    @Column(name = "reprieve_total", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("暂缓合计")
    private double reprieveTotal = 0;    //暂缓合计

    @Column(name = "value_total", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("分值合计")
    private double valueTotal = 0; //分值合计

    @Column(name = "amt_total", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("金额合计")
    private double amtTotal = 0;//金额合计

    @Column(name = "no_reprieve_total", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("金额合计(不含暂缓）")
    private double noReprieveTotal = 0; //金额合计(不含暂缓）

    @Column(name = "afford_Loss_Amt_Total", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("承担损失金额合计")
    private double affordLossAmtTotal = 0;//承担损失金额合计

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    private PartyRole company; //公司

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id")
    private Dept dept; // 部门(用于部门只能查询本部门数据)

    @Transient
    private PartyRole _creator;

    @Transient
    public PartyRole get_creator() {
        if(this.getCreator() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getCreator());
    }

    @Transient
    private User _creatorU;

    public User get_creatorU() {
        if(this.getCreator() == null) return null;
        return SpringManager.getBean(UserRepository.class).findByPartyRoleId(this.getCreator()).get(0);
    }
}
